//
//  AlarmStatisticsTopView.swift
//  SBSafetyApp
//
//  Created by Lina on 2023/4/22.
//

import Foundation
import UIKit

class AlarmStatisticsTopView: UIView {
    let summaryV = AlarmSummaryView()

    let segmentedC = ScrollableSegmentedControl()

    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .bg
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - Setup
    
    func setupUI() {
        addSubview(summaryV)
        summaryV.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(54)
        }
        
        segmentedC.tintColor = .primary
        segmentedC.underlineSelected = true
        segmentedC.segmentContentColor = .black
        segmentedC.selectedSegmentContentColor = .primary
        segmentedC.backgroundColor = .white
        let normalAttrs = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)]
        let highlightAttrs =  [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)]
        let selectAttrs = [NSAttributedString.Key.font: UIFont.systemFont(ofSize: 16)]
        segmentedC.setTitleTextAttributes(normalAttrs, for: .normal)
        segmentedC.setTitleTextAttributes(highlightAttrs, for: .highlighted)
        segmentedC.setTitleTextAttributes(selectAttrs, for: .selected)
        addSubview(segmentedC)
        segmentedC.snp.makeConstraints { (make) -> Void in
            make.top.equalTo(summaryV.snp.bottom)
            make.left.right.equalToSuperview()
            make.height.equalTo(48)
            make.bottom.equalToSuperview()
        }
        
        segmentedC.insertSegment(withTitle: "报警信号", at: 0)
        segmentedC.insertSegment(withTitle: "撤布防", at: 1)
        segmentedC.selectedSegmentIndex = 0
    }
}

class AlarmSummaryView: UIView {
    let unarmedV = InspListSummaryCard()
    let armedV = InspListSummaryCard()
    let rateV = InspListSummaryCard()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        setupUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    func update(withData modal: AlarmCbfCountModal?) {
        unarmedV.val = "\(modal?.cfCount ?? 0)"
        armedV.val = "\(modal?.bfCount ?? 0)"
        rateV.val = "\(modal?.parent ?? "0.0")%"
    }
    
    // MARK: - Setup
    
    func setupUI() {
        backgroundColor = .hex("#FCF2ED")
        
        let itemW = ScreenWidth / 3.0

        unarmedV.key = "当前撤防"
        unarmedV.val = "0"
        addSubview(unarmedV)
        unarmedV.snp.makeConstraints { make in
            make.left.equalTo(self.snp.left)
            make.top.equalTo(self.snp.top)
            make.width.equalTo(itemW)
        }
        
        let vLine1 = UIView()
        vLine1.backgroundColor = .hex("#F6DBCE")
        addSubview(vLine1)
        vLine1.snp.makeConstraints { make in
            make.left.equalTo(unarmedV.snp.right)
            make.centerY.equalTo(self.snp.centerY)
            make.height.equalTo(27)
            make.width.equalTo(0.5)
        }
        
        armedV.key = "当前布防"
        armedV.val = "0"
        addSubview(armedV)
        armedV.snp.makeConstraints { make in
            make.left.equalTo(unarmedV.snp.right)
            make.centerY.equalTo(unarmedV.snp.centerY)
            make.width.equalTo(itemW)
        }
        
        let vLine2 = UIView()
        vLine2.backgroundColor = .hex("#F6DBCE")
        addSubview(vLine2)
        vLine2.snp.makeConstraints { make in
            make.left.equalTo(unarmedV.snp.right)
            make.top.equalTo(vLine1.snp.top)
            make.bottom.equalTo(vLine1.snp.bottom)
            make.width.equalTo(vLine1.snp.width)
        }
        
        rateV.key = "布防率"
        rateV.val = "0.0%"
        addSubview(rateV)
        rateV.snp.makeConstraints { make in
            make.left.equalTo(armedV.snp.right)
            make.centerY.equalTo(unarmedV.snp.centerY)
            make.width.equalTo(itemW)
        }
    }
}
